package com.wzw.shoppingcart.service.impl;

import com.wzw.shoppingcart.entity.CartLine;
import com.wzw.shoppingcart.entity.Product;
import com.wzw.shoppingcart.service.DiscountService;
import com.wzw.shoppingcart.service.DiscountPromotionService;
import com.wzw.shoppingcart.service.PromotionService;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

import static com.wzw.shoppingcart.constant.Constant.PRODUCT_DISCOUNT;

/**
 *@author wzw
 * 2023/4/15
 */
@Service
@Slf4j
@Setter(onMethod_ = {@Autowired})
public class SpecifyDiscountServiceImplDiscount implements DiscountPromotionService {

    private PromotionService promotionService;
    private DiscountService discountService;

    @Override
    public BigDecimal calculateDiscount(List<CartLine> cartLines, BigDecimal totalOriginPrice) {

        List<Product> productList = promotionService.promotionProduct(cartLines, PRODUCT_DISCOUNT);

        BigDecimal discountRate = discountService.getByType(PRODUCT_DISCOUNT).getDiscountRate();

        discountService.setProductDiscountPrice(cartLines,productList,discountRate);

        return productList.stream().map(p -> p.getPrice()
                        .multiply(BigDecimal.ONE.subtract(discountRate)))
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }


}
